<template>
  <div>
    <el-row>
      <el-col :span="6">
        <el-button size="mini" @click="queryAllGroup()">全部分组</el-button>
        <el-tree
          :data="materialGroup"
          :props="materialGroupTreeProps"
          node-key="id"
          ref="materialGroupTree"
          @node-click="nodeClick"
          :highlight-current="true"
          :expand-on-click-node="false"
          :default-expand-all="true"
        >
        </el-tree>
      </el-col>
      <el-col :span="18">
        <div>
          <el-form
            size="small"
            :inline="true"
            :model="dataForm"
            @keyup.enter.native="getDataList()"
          >
            <el-form-item>
              <el-input
                v-model="dataForm.name"
                placeholder="文件名"
                clearable
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button @click="getDataList()">查询</el-button>
              <el-button @click="addOrUpdateHandle()">上传文件</el-button>
            </el-form-item>
          </el-form>
          <el-table
            :data="dataList"
            size="mini"
            row-key="id"
            border
            v-loading="dataListLoading"
            @selection-change="selectionChangeHandle"
            style="width: 100%"
          >
            <el-table-column
              type="index"
              align="center"
              label="编号"
              width="80"
            />
            <el-table-column
              prop="name"
              header-align="center"
              align="center"
              label="素材名称"
            >
            </el-table-column>
            <el-table-column
              prop="oldName"
              header-align="center"
              align="center"
              label="素材原名称"
            >
            </el-table-column>

            <el-table-column
              prop="url"
              header-align="center"
              align="center"
              width="100"
              label="素材预览图"
            >
              <template slot-scope="scope">
                <Picture :url="scope.row.url"></Picture>
              </template>
            </el-table-column>
            <el-table-column
              prop="type"
              header-align="center"
              align="center"
              width="100"
              label="素材类型"
            >
            </el-table-column>
            <el-table-column
              prop="dataState"
              header-align="center"
              align="center"
              width="100"
              label="状态"
            >
              <template slot-scope="scope">
                <el-tag
                  v-if="scope.row.dataState === -1"
                  size="mini"
                  type="danger"
                  >禁用</el-tag
                >
                <el-tag v-else size="mini">正常</el-tag>
              </template>
            </el-table-column>
            <el-table-column
              prop="createTime"
              header-align="center"
              align="center"
              width="180"
              label="创建时间"
            >
            </el-table-column>
            <el-table-column
              fixed="right"
              header-align="center"
              align="center"
              width="150"
              label="操作"
            >
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="mini"
                  @click="deleteHandle(scope.row.id)"
                  >下载</el-button
                >

                <el-button
                  type="text"
                  size="small"
                  @click="deleteHandle(scope.row.id)"
                  >删除</el-button
                >
                <el-button
                  v-if="scope.row.dataState === -1"
                  type="text"
                  size="small"
                  @click="enableHandle(scope.row.id)"
                  >启用</el-button
                >
                <el-button
                  v-if="scope.row.dataState === 0"
                  type="text"
                  size="small"
                  @click="disableHandle(scope.row.id)"
                  >冻结</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :current-page="pageIndex"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            :total="totalPage"
            layout="total, sizes, prev, pager, next, jumper"
          >
          </el-pagination>
        </div>
      </el-col>
    </el-row>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./add-or-update";
import {
  getMaterialPageList,
  enableMaterial,
  disableMaterial,
  getMaterialGroupList,
} from "@/api/product";
import { treeDataTranslate } from "@/utils";
import Picture from "@/components/picture";
export default {
  data() {
    return {
      dataForm: {
        name: "",
        groupId: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      materialGroup: [],
      materialGroupTreeProps: {
        label: "name",
        children: "children",
      },
    };
  },
  watch: {},
  components: {
    AddOrUpdate,
    Picture,
  },
  activated() {
    this.getMaterialGroupList();
    this.getDataList();
  },
  methods: {
    queryAllGroup() {
      this.dataForm.groupId = "";
      this.getDataList();
    },
    nodeClick(data, node) {
      this.dataForm.groupId = data.id;
      this.getDataList();
    },
    // 获取素材库分组数据列表
    getMaterialGroupList() {
      getMaterialGroupList({}).then((res) => {
        if (res && res.code === 0) {
          this.materialGroup = treeDataTranslate(res.data, "id");
        }
      });
    },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      getMaterialPageList({
        pageNumber: this.pageIndex,
        pageSize: this.pageSize,
        groupId: this.dataForm.groupId,
        name: this.dataForm.name,
      }).then((res) => {
        if (res && res.code === 0) {
          this.dataList = res.data.data;
          this.totalPage = res.data.data.count;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(data) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(data);
      });
    },
    enableHandle(id) {
      this.$confirm(`确定进行启用操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          enableMaterial({ id: id }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    disableHandle(id) {
      this.$confirm(`确定进行冻结操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          disableMaterial({ id: id }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.id;
          });
      this.$confirm(`是否确定进行删除操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          removeMaterial({ ids: ids }).then((res) => {
            if (res && res.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(res.msg);
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="scss">
</style>